﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Navigation;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;

namespace Songhay.Silverlight.BiggestBox.IndexPart.ViewModels
{
    using Songhay.Models;
    using Songhay.Mvvm.Models;
    using Songhay.Mvvm;
    using Songhay.Mvvm.ViewModels;
    using Songhay.Mvvm.Views;
    using Views;

    /// <summary>
    /// Packed XAML View Model
    /// </summary>
    public class PackedXamlViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PackedXamlViewModel"/> class.
        /// </summary>
        /// <remarks>
        /// For details, see
        /// “How to: Implement Property Change Notification”
        /// http://msdn.microsoft.com/en-us/library/ms743695.aspx
        /// A sample <c>set</c> block:
        /// <code>
        /// set
        /// {
        /// _myProperty = value;
        /// OnPropertyChanged("MyProperty");
        /// }
        /// </code>
        /// </remarks>
        public PackedXamlViewModel()
        {
            this.Resources = ApplicationUtility
                .ListPackedResources(Assembly.GetExecutingAssembly());

            #region commanding:

            this.LoadPackedXamlCommand = new RelayCommand<string>(param =>
            {
                var location = string.Format("/sample/packedxaml/{0}", param);
                var message = new LightNavigationMessage<string>(location, "LoadPackedXamlCommand");
                Messenger.Default.Send(message);
            });

            #endregion

            #region messaging:

            Messenger.Default.Register<LightNavigationMessage<NavigationEventArgs>>(this,
            message =>
            {
                if(message.Source != LightMessageSource.ViewNavigatedTo) return;
                if(message.UniqueId != "PackedXamlPage") return;

                var notificationAction = new LightNotificationMessageAction<string>(message.UniqueId,
                pageParameter =>
                {
                    if(string.IsNullOrEmpty(pageParameter)) return;

                    pageParameter = string.Format("/Songhay.Silverlight.BiggestBox.IndexPart;Component/packedxaml/{0}.xaml", pageParameter);

                    var uri = new Uri(pageParameter, UriKind.Relative);
                    var collectionItem = this.Resources.First(r =>
                    {
                        var resourceLocation = new Uri(r.Key, UriKind.Relative);
                        return resourceLocation.Equals(uri);
                    });

                    var resourceOfT = ApplicationUtility
                        .GetFromResourceStream<UserControl>(collectionItem);

                    var window = new PackedXamlChildWindow(
                        resourceOfT.XamlObject,
                        resourceOfT.XamlString);
                    window.Title = collectionItem.DisplayName;
                    window.Show();
                })
                {
                    Source = LightMessageSource.ViewModel,
                    UniqueId = "SamplesList"
                };
                Messenger.Default.Send(notificationAction);
            });

            #endregion
        }

        /// <summary>
        /// Gets the load packed XAML command.
        /// </summary>
        /// <value>The load packed XAML command.</value>
        public ICommand LoadPackedXamlCommand { get; private set; }

        /// <summary>
        /// Gets or sets the collection of loose XAML resources.
        /// </summary>
        /// <value>The resources.</value>
        public IEnumerable<PackedResource> Resources { get; private set; }
    }
}
